package com.wigem.approve.service;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wigem.framework.common.pojo.PageResult;
import com.wigem.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.wigem.approve.entity.DaqEnterpriseEntity;
import com.wigem.approve.mapper.DaqEnterpriseMapper;
import com.wigem.approve.vo.DaqEnterpriseVo;
import com.wigem.framework.sa.token.core.UserInfo;
import com.wigem.framework.sa.token.core.util.SaTokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 楼宇入驻企业填报Service业务层处理
 *
 * @author DH
 * @date 2024-05-26
 */
@Service
public class DaqEnterpriseServiceImpl implements DaqEnterpriseService {

    @Resource
    private DaqEnterpriseMapper daqEnterpriseMapper;


    @Override
    public List<DaqEnterpriseEntity> getList(DaqEnterpriseVo daqEnterprisevo) {
        return daqEnterpriseMapper.selectList(query(daqEnterprisevo));
    }

    @Override
    public PageResult<DaqEnterpriseEntity> getPageList(DaqEnterpriseVo daqEnterpriseVo) {
        Page<DaqEnterpriseEntity> page = new Page<>(daqEnterpriseVo.getPageNo(), daqEnterpriseVo.getPageSize());
        IPage<DaqEnterpriseEntity> mappingList = daqEnterpriseMapper.getPageList(page, daqEnterpriseVo);
        return new PageResult<>(mappingList.getRecords(), mappingList.getTotal());
    }
    @Override
    public PageResult<DaqEnterpriseEntity> getManagePageList(DaqEnterpriseVo daqEnterpriseVo) {
        Page<DaqEnterpriseEntity> page = new Page<>(daqEnterpriseVo.getPageNo(), daqEnterpriseVo.getPageSize());
        IPage<DaqEnterpriseEntity> mappingList = daqEnterpriseMapper.getManagePageList(page, daqEnterpriseVo);
        return new PageResult<>(mappingList.getRecords(), mappingList.getTotal());
    }


    @Override
    public List<DaqEnterpriseEntity> getExportPageList(DaqEnterpriseVo daqEnterpriseVo) {
        return  daqEnterpriseMapper.getExportPageList(daqEnterpriseVo);
    }


    @Override
    public DaqEnterpriseEntity getEntity(String id) {
        return daqEnterpriseMapper.selectById(id);
    }

    @Override
    public void add(DaqEnterpriseVo daqEnterpriseVo) {
        DaqEnterpriseEntity daqEnterpriseEntity = new DaqEnterpriseEntity();
        BeanUtils.copyProperties(daqEnterpriseVo, daqEnterpriseEntity);
        daqEnterpriseEntity.create();
        daqEnterpriseMapper.insert(daqEnterpriseEntity);
    }


    @Override
    public void update(DaqEnterpriseVo daqEnterpriseVo) {
        DaqEnterpriseEntity daqEnterpriseEntity = new DaqEnterpriseEntity();
        BeanUtils.copyProperties(daqEnterpriseVo, daqEnterpriseEntity);
        daqEnterpriseMapper.updateById(daqEnterpriseEntity);
    }

    @Override
    public void save(DaqEnterpriseVo daqEnterpriseVo) {

        DaqEnterpriseEntity daqEnterpriseEntity = new DaqEnterpriseEntity();
        BeanUtils.copyProperties(daqEnterpriseVo, daqEnterpriseEntity);
        UserInfo userInfo = SaTokenUtils.getLoginUserInfo();
        if (StringUtils.isEmpty(daqEnterpriseVo.getId())) {
            daqEnterpriseEntity.create();
            daqEnterpriseEntity.setCreateUser(userInfo.getUserId());
            daqEnterpriseEntity.setCreateTime(new Date());
            daqEnterpriseMapper.insert(daqEnterpriseEntity);
        } else {
            daqEnterpriseMapper.updateById(daqEnterpriseEntity);
        }

    }


    @Override
    public int delete(String id) {
        return daqEnterpriseMapper.deleteById(id);
    }


    @Override
    public int remove(String id) {
        return daqEnterpriseMapper.remove(id);
    }
    @Override
    public int removeByPolicyId(String id) {
        return daqEnterpriseMapper.removeByPolicyId(id);
    }


    public LambdaQueryWrapperX<DaqEnterpriseEntity> query(DaqEnterpriseVo daqEnterpriseVo) {

        LambdaQueryWrapperX<DaqEnterpriseEntity> daqEnterpriseEntityLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        daqEnterpriseEntityLambdaQueryWrapperX = daqEnterpriseEntityLambdaQueryWrapperX
                .eqIfPresent(DaqEnterpriseEntity::getId, daqEnterpriseVo.getId())
                .eqIfPresent(DaqEnterpriseEntity::getSocialCreditCode, daqEnterpriseVo.getSocialCreditCode())
                .likeIfPresent(DaqEnterpriseEntity::getOrgName, daqEnterpriseVo.getOrgName())
                .eqIfPresent(DaqEnterpriseEntity::getRegTime, daqEnterpriseVo.getRegTime())
                .eqIfPresent(DaqEnterpriseEntity::getOrgRegAddr, daqEnterpriseVo.getOrgRegAddr())
                .eqIfPresent(DaqEnterpriseEntity::getUseProperty, daqEnterpriseVo.getUseProperty())
                .eqIfPresent(DaqEnterpriseEntity::getOfficeArea, daqEnterpriseVo.getOfficeArea())
                .eqIfPresent(DaqEnterpriseEntity::getCheckInTime, daqEnterpriseVo.getCheckInTime())
                .eqIfPresent(DaqEnterpriseEntity::getRentTime, daqEnterpriseVo.getRentTime())
                .eqIfPresent(DaqEnterpriseEntity::getReportId, daqEnterpriseVo.getReportId())
                .eqIfPresent(DaqEnterpriseEntity::getFillPerson, daqEnterpriseVo.getFillPerson())
                .eqIfPresent(DaqEnterpriseEntity::getFillPhone, daqEnterpriseVo.getFillPhone())
                .eqIfPresent(DaqEnterpriseEntity::getCreateUser, daqEnterpriseVo.getCreateUser())
                .eqIfPresent(DaqEnterpriseEntity::getRemoveMark, daqEnterpriseVo.getRemoveMark())
                .eqIfPresent(DaqEnterpriseEntity::getPolicyId, daqEnterpriseVo.getPolicyId())

        ;
        return daqEnterpriseEntityLambdaQueryWrapperX;
    }


}
